package com.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class CurrentUser {
    //需要提前在拦截器中配置，单独使用应当赋值。
    public static String token = null;

    /**
     * 动态获取eid
     *
     * @return
     */
    public Integer getCurrentEid() {
        try {
            Integer eid = JwtUtil.getEid(token);
            return eid;
        } catch (Exception e) {
            log.info(e.getMessage());
            throw new RuntimeException("解析当前用户异常");
        }
    }

    /**
     * 动态获取ename
     *
     * @return
     */
    public String getCurrentEname() {
        try {
            String ename = JwtUtil.getEname(token);
            return ename;
        } catch (Exception e) {
            log.info(e.getMessage());
            throw new RuntimeException("解析当前用户异常");
        }
    }

    /**
     * 比较用户信息
     *
     * @param ename
     * @param eid
     * @return
     */
    public boolean eq(String ename, Integer eid) {
        Integer currentEid = this.getCurrentEid();
        String currentEname = this.getCurrentEname();
        if (currentEid != null && eid != null && currentEname != null && ename != null) {
            return currentEid.equals(eid) == currentEname.equals(ename) ? true : false;
        }
        return false;
    }
}
